home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 312.adf / TrackSalve / Source / startup.a < prev    next >
Text File  |  1990-02-12  |  10KB  |  203 lines

  1. *
  2. * C initial startup procedure under AmigaDOS for usage with Lattice 5.04
  3. * Special stripped version !!
  4. *
  5.  
  6. *               INCLUDE "exec/types.i"
  7.                 INCLUDE "libraries/dosextens.i"
  8.                 INCLUDE "call.i"
  9.                 INCLUDE "globdefs.i"
  10.  
  11. AbsExecBase     EQU     4
  12.  
  13.  
  14. *-------------------------------------------------------*
  15. *
  16. *  StartUp
  17. *
  18. *  Highest level routine of a CLI-based program, a limited interface between system and application.
  19. *
  20. *  Input:       4(a7)   Stacksize (if called from CLI)
  21. *               a0      Commandline parameter string
  22. *               d0      Commandline parameter length
  23. *
  24. *  Output:      d0      Return value (as can be used in script files)
  25. *               (pr_result2  to pass DOS errors for usage with "Why")
  26. *
  27. *  Usage:       a3      Pointer to our Process structure
  28. *               a4      Pointer to our global data area
  29. *               a5      Stack marker
  30. *               a6      Library pointers
  31. *
  32.  
  33.  
  34.                 xdef    _XCEXIT                         Exit() is standard way to leave C. (Not here)
  35.  
  36.                 xref    _LinkerDB                       Linker defined base value
  37.                 xref    __BSSBAS                        Linker defined base of BSS
  38.                 xref    __BSSLEN                        Linker defined length of BSS
  39.  
  40.                 CSECT   text,0,0,1,2                    Any xref's after this are 16-bit reloc
  41.  
  42.                 Func    _StartUp
  43.                 movem.l d1-d7/a0-a6,-(a7)
  44. REGSIZE         EQU     (7+7)*4
  45.                 lea     REGSIZE(a7),A5                  Determine old stack pointer, it has some parameters
  46.                 move.l  a0,a2                           Save commandline parameter pointer
  47.                 move.l  d0,d2                           And commandline parameter length
  48.  
  49. *--     Set BSS to zero
  50.  
  51.                 lea     __BSSBAS,a0                     Get base of BSS
  52.                 move.l  #__BSSLEN,d0                    Get length of BSS in longwords
  53.                 moveq.l #0,d1
  54.                 bra.s   ClearLoop                       And clear for length given
  55. ClearBSS        move.l  d1,(a0)+
  56. ClearLoop       dbf     d0,ClearBSS                     Loop..
  57.  
  58.  
  59. *--     Load baseregister and init some importand globals
  60.  
  61.                 lea     _LinkerDB,a4                    Load base register
  62.                 move.l  a7,__StackPtr(a4)               Save stack ptr
  63.                 move.l  AbsExecBase.w,a6                Exec pointer
  64.                 move.l  a6,_SysBase(A4)
  65.                 lea     DOSName(pc),A1                  "dos.library"
  66.                 moveq.l #0,D0                           Version number
  67.                 LibCall OpenLibrary                     Open DOS-library
  68.                 move.l  D0,_DOSBase(A4)                 Library base pointer, success?
  69.                 bne.s   DOSok                           Yes..
  70.                 moveq.l #100,d0                         No, how did we get here anyway?
  71.                 bra     Exit2                           Leave..
  72. DOSok
  73.  
  74. *--     Get the address of our task
  75.  
  76.                 suba.l  a1,a1                           0-> a1 for own task
  77.                 LibCall FindTask                        Findtask() used as FindProcess()
  78.                 move.l  d0,a3                           Pointer to struct Process
  79.                 move.l  d0,_OurTask(a4)                 Save for later and other users
  80.  
  81. *--     Are we running as a child of Workbench?  Leave if so..
  82.  
  83.                 move.l  pr_CurrentDir(A3),_curdir(A4)   Lock associated with current directory
  84.                 tst.l   pr_CLI(A3)                      Pointer to ConsoleLineInterpreter
  85.                 bne.s   FromCLI                         We are from the CLI, process CLI startup..
  86.                 lea.l   pr_MsgPort(a3),a0               Port Workbench will send us it's startupmessage
  87.                 LibCall WaitPort                        Wait for it
  88.                 lea.l   pr_MsgPort(a3),a0               Port Workbench has send us it's startupmessage
  89.                 LibCall GetMsg                          Remove message from port
  90.                 move.l  d0,d3                           Keep message
  91.                 LibCall Forbid                          No task-switching to prevent Workbench to UnLoadSeg() us
  92.                 move.l  d3,a1                           Workbench startup message
  93.                 LibCall ReplyMsg                        Reply message to tell Workbench we have terminated
  94.                 bra     Exit4                           Leave..
  95.  
  96. FromCLI
  97.  
  98. *--     Calculate available stackspace
  99.  
  100.                 move.l  a5,D0                           Get top of stack
  101.                 sub.l   4(a5),D0                        Compute bottom
  102.                 add.l   #128,D0                         Allow for parms overflow
  103.                 move.l  D0,__base(A4)                   Save for dynamic stack checking
  104.  
  105. *--     Find command name
  106.  
  107.                 move.l  pr_CLI(a3),a0                   BCPL-pointer to ConsoleLineInterpreter
  108.                 add.l   a0,a0                           Pointer conversion
  109.                 add.l   a0,a0
  110.                 move.l  cli_CommandName(a0),a1          BCPL-pointer to name of current command
  111.                 add.l   a1,a1                           Pointer conversion
  112.                 add.l   a1,a1
  113.  
  114. *--     Collect parameters
  115.  
  116.                 move.l  d2,d0                           Command line parameters length
  117.                 moveq.l #0,d1
  118.                 move.b  (a1)+,d1                        First byte BCPL string is length
  119.                 move.l  a1,__ProgramName(A4)            Updated string pointer
  120.                 add.l   d1,d0                           Add length of command name
  121.                 addq.l  #1,d0                           Allow for space after command
  122.  
  123.                 clr.w   -(A7)                           Set null terminator for command line
  124.                 addq.l  #1,D0                           Force to even number of bytes
  125.                 andi.w  #$fffe,D0                       Round up
  126.                 sub.l   D0,A7                           Make room on stack for command line
  127.                 subq.l  #2,D0
  128.                 clr.w   0(A7,D0)
  129.  
  130. *--     Copy command line onto stack
  131.  
  132.                 move.l  d2,d0                           Command line parameters length
  133.                 subq.l  #1,d0
  134.                 add.l   d1,d2                           Add length of command name
  135.  
  136. CopyLine        move.b  0(A2,D0.W),0(A7,D2.W)           Copy command line parameters to stack
  137.                 subq.l  #1,d2                           Post decrement
  138.                 dbf     d0,CopyLine                     Loop..
  139.                 move.b  #' ',0(a7,d2.w)                 Add space between command and parms
  140.                 subq.l  #1,d2                           Post decrement
  141. CopyCommand     move.b  0(a1,d2.w),0(a7,d2.w)           Copy command name to stack
  142.                 dbf     d2,CopyCommand                  Loop..
  143.                 move.l  A7,A1                           Commandline address
  144.                 move.l  A1,-(A7)                        Push command line address
  145.  
  146. *--     Open low level standard error in the current window
  147.  
  148.                 lea.l   Asterix(pc),a0                  "*" Current window name
  149.                 move.l  a0,d1                           Name parameter
  150.                 move.l  #MODE_OLDFILE,d2                AccessMode parameter
  151.                 DosCall Open                            Open output
  152.                 move.l  d0,_ConOut(a4)                  Make public
  153.                 beq.s   Exit4                           No stderr, leave..
  154.  
  155. *--     Call application or next stage of interface
  156.  
  157. Main            Call    __main                          Call C entrypoint
  158.                 ;moveq.l #0,d0                          Set successful status (we donot: int _main(), main();)
  159.                 bra.s   Exit2                           Skip exit() stack argument..
  160.  
  161. *--     exit() entry-point.  All our registers are unreliable.
  162.  
  163. _XCEXIT         move.l  4(a7),d0                        Extract return code from stack
  164. Exit2           move.l  d0,d7                           Keep return code
  165.                 lea     _LinkerDB,a4                    Base register. Could have been trashed when called Exit()... Yes?
  166.                 move.l  __StackPtr(a4),a7
  167. Exit3           ;Call   _MemCleanup                     Let's not
  168.  
  169. *--     Close stderr
  170.  
  171.                 move.l  _ConOut(a4),d1                  File handle to stderr
  172.                 DosCall Close                           Close stderr
  173.  
  174. Exit4           move.l  _OurTask(a4),a3                 Restore a3 as Processpointer
  175.                 move.l  _R2(a4),pr_Result2(a3)          Pass some extra error info
  176.                 move.l  d7,d0                           Return code
  177.                 movem.l (a7)+,d1-d7/a0-a6
  178.                 rts
  179.  
  180.  
  181. DOSName         dc.b    'dos.library',0
  182. Asterix         dc.b    '*',0
  183.  
  184.  
  185.  
  186. *-------------------------------------------------------*
  187.  
  188.  
  189.                 csect   __MERGED,1,,2,2
  190.  
  191.                 gs_l    __StackPtr,1
  192.                 gs_l    _OurTask,1
  193.                 gs_l    _SysBase,1
  194. *               gs_l    _DOSBase,1
  195.                 gs_l    _ConOut,1
  196.                 gs_l    _R2,1                   Secondary result (DOS-Why program)
  197.                 gs_l    __base,1                Base of stack
  198.                 gs_l    __ProgramName,1
  199.                 gs_l    _curdir,1
  200.  
  201.                 END
  202.  
  203.